package br.ucb.ucblive.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.ucb.ucblive.beans.Aluno;
import br.ucb.ucblive.beans.Curso;
import br.ucb.ucblive.beans.Moderador;
import br.ucb.ucblive.beans.Professor;

public class ProfessorHIB{

	@SuppressWarnings("unchecked")
	public List<Professor> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Professor.class).list();
		} finally {
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Professor> listarOrdemCrescente() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Professor.class).
				   addOrder(Order.asc("this.nome")).
				   list();
		} finally {
			session.close();
		}
	}
	
	public void salvar(Professor obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(obj);
		transaction.commit();
		session.close();
	}
	
	
	public Professor consultar(int idProfessor) {
		Session session = HibernateUtil.getSession();
		try {
			Professor obj = (Professor) session.get(Professor.class,idProfessor);
			return obj;
		} finally {
			session.close();
		}
	}

	public void excluir(Professor obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(obj);
		transaction.commit();
		session.close();
		
	}

	public Professor consultarByLogin(String login) {
		Session session = HibernateUtil.getSession();
		try {
			Professor obj = (Professor) session.createCriteria(Professor.class).
						add(Restrictions.eq("this.usuario.login", login)).
						setMaxResults(1).
						uniqueResult();
			return obj;
		} finally {
			session.close();
		}
	}
	
	public Professor consultarByMatricula(String matricula) {
		Session session = HibernateUtil.getSession();
		try {
			Professor obj = (Professor) session.createCriteria(Professor.class).
						add(Restrictions.eq("this.matricula", matricula)).
						setMaxResults(1).
						uniqueResult();
			return obj;
		} finally {
			session.close();
		}
	}
}
